
var wsClient = null;
var sessionId = genUUID();
function connWs(wsServer, onError) {
	var client = new WsClient({
	    login: wsAccount,
	    passwd: wsSecret,
	    socketUrl: wsServer,
	    sessid: sessionId,
	    onmessage: function(e) {
			handleMsg(e.eventData);
	    },
	    onWSConnect: function(o) {
			client.call('login', {});
	    },
	    onWSLogin: function(success) {
	    },
	    onWSError: function(e) {
	    	if (onError) onError(e);
	    },
	    onWSClose: function(success) {
	    }
	});
	return client;
}

function procMsg(msg, txt = '') {
	var pattern = /[，。！？：；、,.!?:;]/;
	var msgs = msg.split(pattern);
	if (msgs.length <= 1) return;
	var txts = txt.split(pattern);
	msgs[0] = txts[txts.length - 1] + msgs[0];
	var len = msgs.length;
	for (var i = 0; i < len - 1; i++) {
		var sub = msgs[i] + ",";
		//console.log(sub);
		if (liveClient) {
		    var params = {
	    		msg: {
	        		body: sub
	        	}
		    };
		    liveClient.call("verto.info", params, function(e) {
		    }, function(e) {
		    });
		}
	}
}

//接收消息
function recvMsg(msg, id) {
    var msgHead = "AI";
    if ($('#msg'+id).length > 0) {
    	var txt = $('#msg'+id).text();
    	$('#msg'+id).text(txt + msg);
    	procMsg(msg, txt);
    } else {
	    var content = '<div id="msg' + id + '" style="text-align:right;">' + msgHead + ':' + msg + '</div><br/>';
	    $('#msgDiv').append(content);
    	procMsg(msg);
    }
    $("#msgDiv").scrollTop($("#msgDiv")[0].scrollHeight);
}
//处理消息
function handleMsg(data) {
	if (data.method == "verto.info") {
		if (liveClient) {
			setTimeout(function(){
	    		$('#wordDiv').text(data.params.msg.body);
	    	}, liveDelay);
		} else {
			recvMsg(data.params.body, data.id);
		}
	}
}
//发送消息
function sendMsg() {
    var msg = $('#txtMsg').val();
    if (!msg) {
        $('#txtMsg').focus();
        return;
    }
    if (!wsClient) {
    	wsClient = connWs(wsServer, function(e) {
    		wsClient = connWs(wsServer);
    	});
    }
    var params = {
    	msg: {
        	body: msg
    	}
    };
    var webClient = wsClient.socketReady() ? wsClient : liveClient;
    if (webClient) {
	    webClient.call("verto.info", params, function(e) {
		    var msgHtml = '<div>我:' + msg + '</div><br/>';
		    $('#msgDiv').append(msgHtml);
		    $("#msgDiv").scrollTop($("#msgDiv")[0].scrollHeight);
		    $('#txtMsg').val("");
	    }, function(e) {
	    });
    }
}

var liveClient = null;
var flvPlayer = null;
var flvAudio = null;
//开始播放
function startPlayer() {
	liveClient = connWs(pushServer);
    liveUrl = liveServer+"video-"+sessionId+".flv";
    console.log(liveUrl);
    flvPlayer = mpegts.createPlayer({type: 'flv', url: liveUrl, isLive: true, enableStashBuffer: false});
	flvPlayer.attachMediaElement(document.getElementById('youVideo'));
	flvPlayer.load();
	flvPlayer.play();
    audioUrl = liveServer+"audio-"+sessionId+".flv";
    console.log(audioUrl);
    flvAudio = mpegts.createPlayer({type: 'flv', url: audioUrl, isLive: true, enableStashBuffer: false});
	flvAudio.attachMediaElement(document.getElementById('youAudio'));
	flvAudio.load();
	flvAudio.play();
}
//停止播放
function stopPlayer() {
	liveClient.closeSocket();
	flvPlayer.pause();
    flvPlayer.destroy();
    flvPlayer = null;
	flvAudio.pause();
    flvAudio.destroy();
    flvAudio = null;
}
//视频开关
function openVideo() {
	if ($('#tdVideo').is(':hidden')) {
	    $('#msgDiv').hide();
	    $('#tdVideo').show();
	    if (!flvPlayer) {
	    	startPlayer();
		}
	    if (!$('#tdVideo').height()) {
	        $('#tdVideo').height(videoHeight);
	    }
	    $('#tdWord').show();
	    $('#wordDiv').text("");
		$('#btnVideo').val('关闭');
    } else {
	    $('#tdVideo').hide();
	    $('#msgDiv').show();
        if (flvPlayer) {
        	stopPlayer();
        }
	    $('#tdWord').hide();
		$('#btnVideo').val('视频');
    }
}